export const getIndex = (target: any): any => {
  if (target) {
    return target.hasOwnProperty("parent")
      ? getIndex(target.parent)
      : target.__ecComponentInfo.index + 1;
  } else {
    return false;
  }
};

//为图表计算高度
export const chartssize = (container: any, charts: any) => {
  function getStyle(el: any, name: any) {
    if (window.getComputedStyle) {
      return window.getComputedStyle(el, null);
    } else {
      return el.currentStyle;
    }
  }
  const wi = getStyle(container, "width").width;
  const hi = getStyle(container, "height").height;
  console.log(wi, hi);
  charts.style.height = hi;
  charts.style.width = wi;
};

// 函数节流
export const throttle = (func: any, delay = 1000): any => {
  let lock = false;
  let s: any = null;
  console.log(1111);
  if (lock) {
    return false;
  }
  func();
  lock = true;
  clearTimeout(s);
  s = setTimeout(() => {
    clearTimeout(s);
    lock = false;
  }, delay);
};

// 函数防抖
export const debounce = function(func: any, delay = 1000): any {
  // 记录上一次的延时器
  let timer: any = null;
  return () => {
    // let args: any = arguments;
    // let that: any = this;
    // 清除上一次延时器
    clearTimeout(timer);
    timer = setTimeout(function() {
      func();
    }, delay);
  };
};

// 获取当前时间
// type: 时间格式
export const getNowTime = (type: string): string => {
  let date: any = new Date();
  let timeObj: any = {
    yy: date.getYear(), //获取当前年份(2位)
    yyyy: date.getFullYear(), //获取完整的年份(4位)
    MM: date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
    dd: date.getDate(), //获取当前日(1-31)
    week: date.getDay() + 1, //获取当前星期X(0-6,0代表星期天)
    timestamp: date.getTime(), //获取当前时间(从1970.1.1开始的毫秒数)
    hh: date.getHours(), //获取当前小时数(0-23)
    mm: date.getMinutes(), //获取当前分钟数(0-59)
    ss: date.getSeconds(), //获取当前秒数(0-59)
    // date.getMilliseconds(), //获取当前毫秒数(0-999)
    time1: date.toLocaleDateString(), //获取当前日期
    mytime: date.toLocaleTimeString(), //获取当前时间
    time2: date.toLocaleString() //获取日期与时间
  };
  let typeArr = type.split(":");
  let res: string = "";
  for (let i in typeArr) {
    // console.log(i, typeArr[i], typeArr.length, parseInt(i) + 1);
    if (parseInt(i) + 1 === typeArr.length) {
      res += timeObj[typeArr[i]];
      break;
    } else {
      res += timeObj[typeArr[i]] + ":";
    }
  }
  console.log(res);
  return res;
};
